package com.shuoyutech.dp.framework.filter;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j(topic = "INTERFACE")
@Configuration
public class RequestFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        log.info("[request] getRequestURL: {}", httpServletRequest.getRequestURL());
        log.info("[request] getMethod: {}", httpServletRequest.getMethod());
        log.info("[request] getRemoteAddr: {}", httpServletRequest.getRemoteAddr());
        log.info("[request] getProtocol: {}", httpServletRequest.getProtocol());
        log.info("[request] getParameterMap: {}", JSON.toJSON(httpServletRequest.getParameterMap()));
        log.info("[request] getQueryString: {}", httpServletRequest.getQueryString());

        filterChain.doFilter(httpServletRequest, httpServletResponse);

        log.info("[response] getContentType: {}", httpServletResponse.getContentType());
    }
}
